<template>
  <div>
    <el-dialog :title="info.isAdd ? '轮播图添加' : '轮播图修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="标题" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img = $event" ref="upload"></upload-img>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqbanneradd, reqbannerinfo, reqbanneredit } from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        title: "",
        img: null,
        status: 1
      },
      imgUrl: ""
    };
  },
  computed: {
    ...mapGetters({
      arr: "banner/arr"
    }),
    showArr() {
      return this.arr.filter(item => item.id != this.user.id);
    }
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "banner/reqArr"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.title == "") {
          errorAlert("轮播图标题不能为空");
          return;
        }
        if (this.user.img == null) {
          errorAlert("轮播图图片不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(res => {
        reqbanneradd(this.user).then(() => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    empty() {
      this.user = {
        title: "",
        img: null,
        status: 1
      };
      this.$refs.upload.setImgUrl("");
    },
    getOne(id) {
      reqbannerinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
          this.$refs.upload.setImgUrl(this.$pre + this.user.img);
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        reqbanneredit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @primary-light;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
  input {
    opacity: 0;
  }
}
</style>